package com.hongli.blog.controller;

import com.hongli.blog.config.Result;
import com.hongli.blog.entity.BasicsDao;
import com.hongli.blog.mapper.Basicsmapper;
import com.hongli.blog.scheduledtask.InitializationController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author ZERO
 * @Date 2021/11/20 17:24
 **/
@RestController
@RequestMapping(value = "/Theme")
@CrossOrigin
public class ThemeController {
    @Autowired
    private Basicsmapper basicsmapper;

    private InitializationController INITI = new InitializationController();

    /** 返回主题列表
     * @author ZERO
     * @date 2021/11/17 15:12
     **/
    @GetMapping(value = "/getThemes")
    public Result getTheme(){
        List<BasicsDao> listbasics = basicsmapper.SelectAllBasics();
        if(listbasics.size() <= 0){
            return Result.fail("2000");
        }
        ArrayList<Object> mapcount = new ArrayList<>();
        for(BasicsDao b : listbasics){
            HashMap<String,Object> map = new HashMap<>();
            map.put("basicsid",b.getBasicsid());
            map.put("basicsname",b.getBasicsname());
            map.put("author",b.getAuthor());
            map.put("mainTitle",b.getMainTitle());
            map.put("meaningtime",INITI.Times(b.getMeaningtime()));
            map.put("record",b.getRecord());
            map.put("choice",("1".equals(b.getChoice())?"启用":"禁用"));
            mapcount.add(map);
        }
        return Result.fail("2000",mapcount);
    }

    /**
     * 添加 / 修改
     * @param map
     * @return
     */
    @PostMapping(value = "/InsertThemes")
    public Result InsertThemes(@RequestBody Map<String,Object> map){
        String T = map.get("T").toString().trim(),
                A = map.get("A").toString().trim(),
                L = map.get("L").toString().trim(),
                R = map.get("R").toString().trim(),
                I = map.get("I").toString().trim();
        if(T.length() <= 0 || A.length() <= 0 || L.length() <= 0 || R.length() <= 0){
            return Result.fail("1000");
        }
        if(I.length() > 0){
            if(Integer.valueOf(I) > 0){
                BasicsDao basicsDao = new BasicsDao(Integer.valueOf(I),T,A,L,INITI.Processing_Time(),R,"0");
                basicsmapper.UpdataBasicsData(basicsDao);
                return Result.fail("2000");
            }
        }
        BasicsDao basicsDao = new BasicsDao(0,T,A,L,INITI.Processing_Time(),R,"0");
        basicsmapper.InsertBasics(basicsDao);
        return Result.fail("2000");
    }

    /**
     * 回填
     * @param maps
     * @return
     */
    @PostMapping(value = "/getThemesData")
    public Result DataThemes(@RequestBody Map<String,Object> maps){
        HashMap<String,Object> map = new HashMap<>();
        BasicsDao b = basicsmapper.UpdataBasics(Integer.valueOf(maps.get("I").toString()));
        map.put("basicsname",b.getBasicsname());
        map.put("author",b.getAuthor());
        map.put("mainTitle",b.getMainTitle());
        map.put("record",b.getRecord());
        return Result.fail("2000",map);
    }

    /**
     * 启用否
     * @param maps
     * @return
     */
    @PostMapping(value = "/ALLTheme")
    public Result ALLTheme(@RequestBody Map<String,Object> maps){
        basicsmapper.UpdataAllRemek();
        basicsmapper.UpdataRemek(Integer.valueOf(maps.get("I").toString()));
        return Result.fail("2000");
    }

    /**
     * 删除
     * @param maps
     * @return
     */
    @PostMapping(value = "/DeleteTheme")
    public Result DeleteTheme(@RequestBody Map<String,Object> maps){
        BasicsDao b = basicsmapper.UpdataBasics(Integer.valueOf(maps.get("I").toString()));
        if("1".equals(b.getChoice())) return Result.fail("1000");
        else basicsmapper.deleteBasics(Integer.valueOf(maps.get("I").toString()));
        return Result.fail("2000");
    }
}
